﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapLibrary.Utility
{
    public class TimerUtility
    {
        public delegate void TimedAction();

        public bool Active
        {
            get;
            set;
        }

        public int Duration
        {
            get;
            set;
        }

        public int NumTicks
        {
            get;
            set;
        }

        public TimedAction Action
        {
            get;
            set;
        }

        public TimerUtility(TimedAction action, int duration)
        {
            Duration = duration;
            NumTicks = 0;
            Action = action;
            Active = true;
        }

        public TimerUtility()
            : this(null, 0)
        {

        }

        public void Update()
        {
            if (Action == null || Duration == 0)
                throw new Exception("Timer isn't initialized with proper values");
            
            if (!Active) return;
            NumTicks++;

            if (NumTicks >= Duration)
            {
                NumTicks = 0;
                Action();
            }
        }
    }
}
